#!/bin/bash

#//////////////////////////////////////////////////////////////////////////////////
#
#  Betsu-Banner v0.1.5-exp | The Betsu Project
#
#//////////////////////////////////////////////////////////////////////////////////
#
#  Copyright (c) 2022, Alexa Hattori <unixcat@anche.no>.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
#  * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
#  * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
#  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#//////////////////////////////////////////////////////////////////////////////////

# Variables
version="v0.1.5-exp"

os_check () {
    case $(uname) in
	"Linux" ) system="GNU"
		  ;;
	"OpenBSD" ) system="OpenBSD"
		    ;;
    esac
}

load_core () {
    source $HOME/.betsu/banner/banner.cfg # Main Configuration File
    
    if [[ force_default == *"true"* ]];
    then
	 system="GNU"
    else
	 os_check
    fi

    system="Guix"

    case $system in
	"GNU" ) source $HOME/.betsu/banner/modules/default.bm;;
	"OpenBSD" ) source $HOME/.betsu/banner/modules/openbsd.bm;;
	"Guix" ) source $HOME/.betsu/banner/modules/guix.bm;;
	* ) echo "Your system is not supported."; exit;;
    esac	
}

modules_install () {
    echo "################################"
    echo "Betsu-Banner v0.1.5-experimental"
    echo "        Modules-Install         "
    echo "################################"
    echo "What do you want to install?"
    echo ""
    select to_install in "Core : Default" "Core : OpenBSD" "Cancel"; do
	case $to_install in
	    "Core : Default" ) install_target="default"; install_module; break;;
	    "Core : OpenBSD" ) install_target="openbsd"; install_module; break;;
	    "Core : Guix" ) install_target="guix"; install_module; break;;
	    "Cancel" ) exit;;
	esac
    done
}

install_module () {
    pushd $HOME/.betsu/banner/modules;
    echo "[ INFO ] Betsu-Banner | Fetching latest $install_target.bm"
    curl -o https://cdn.betsu.org/banner/modules/core/$install_target.bm
    popd;
    echo "[ INFO ] Betsu-Banner | $install_target.bm has installed.";
}

load_core

update () {
    echo "[ INFO ] Betsu-Banner | Checking if update available..."
    latest_version=$(curl -s https://cdn.betsu.org/betsu/banner/c/latest.txt)

    echo "[ DEBUG ] Variable | latest_version : $latest_version"
    echo "[ DEBUG ] Variable | version : $version"
    
    if [[ latest_version == *"v0.1.5-exp"* ]];
    then
	echo "[ INFO ] Betsu-Banner | Latest version is already installed."
    else
	echo "[ INFO ] Betsu-Banner | New version found!"
	echo "[ INFO ] Betsu-Banner | Fetching new version, please wait..."
	# fetch_new_version
    fi
}

generate_config () {
    if [ -d "$HOME/.betsu/banner" ]; then # Check if $HOME/.betsu/banner exist
	echo "0" > /dev/null # Do Nothing
    else
	echo "[ INFO ] Betsu-Banner |" $HOME/.betsu/banner/ "doesn't exists, creating..."  
	mkdir -p -v $HOME/.betsu/banner/modules # Create directory for Betsu configuration files
    fi

    echo "[ INFO ] Betsu-Banner | Generating the Configuration File..."

    echo " 
    # Betsu-Banner $version Configuration File
    # $HOME/.config/betsu/banner.cfg

    # Header Settings
    
    date_format="12" # 12-hour or 24-hour.

    ascii_betsu_logo='false'

    # First Block

    cpu_temp='true'
    ram_usage='true'
    swap_usage='false'
    battery='false'
    uptime='true'
    motd='true'

    # storage

    rootfs='true'
    userspace='true'

    ## Custom
    
    enable_custom_mountpoint='false'
    custom_mountpoint='REPLACE_ME' 

    # Custom Entry

    enable_custom_entry='false' # Enable Custom Entry
    
    print_custom () { # Do not edit function name!
        echo '- Custom : $(echo Hello, world!)' # Any code here
    }

    # Second Block    

    # Network

    lan_ip='true'
    wan_ip4='false'
    wan_ip6='false'

    # Other
   
    modules_enabled='false' # Load modules from $HOME/.betsu/banner/modules/
    update_interval='1' # In seconds, for --loop option" > $HOME/.betsu/banner/banner.cfg
}

if [ -f "$HOME/.betsu/banner/banner.cfg" ] # Check is configuration file exist, if no - run generate_config
then
    echo "0" > /dev/null
else
    echo "[ WARNING ] Betsu-Banner | Configuration File doesn't exist, running generate_config function."
    generate_config
    echo "[ INFO ] Betsu-Banner | Please, run betsu-banner again."
    exit
fi

print_line () { # Line
    echo "====================================================="
}

print_header () { # Prints "Betsu Banner vX.X.X : Date"
    if [[ $date_format == *"12"* ]];
    then
	echo "Betsu Banner" $version ":" $(date +%r) "-" $(date +%D)
    elif [[ $date_format == *"24"* ]];
	 then
	     echo "Betsu Banner" $version ":" $(date +%R) "-" $(date +%D)
	 else
	     echo "[ FAILURE ] Configuration Error: unknown option"
	     echo "Variable: date_format"
	     echo "Available options: 12, 24"
	     echo "[ DEBUG ] Stopped at print_header"
	     exit;
	 fi
}

print_betsu_logo () { # Prints ASCII Betsu Logo
    echo "
   _            _               
  | |__    ___ | |_  ___  _   _
  | '_ \  / _ \| __|/ __|| | | |
  | |_) ||  __/| |_ \__ \| |_| |
  |_.__/  \___| \__||___/ \__,_|
  
  "
}

print_motd () { # Prints MOTD from cdn.betsu.org/betsu/banner/c/motd.txt
   echo " - Motd :" $(curl -s https://cdn.betsu.org/betsu/banner/c/motd.txt)
}

print_once () { # Print all information what is enabled in configuration file.
    print_line # Line
    print_header
    print_line # Line
    
    if [[ $ascii_betsu_logo == *"true"* ]];
    then
	print_betsu_logo
    else
	echo ""
    fi

    if [[ $uptime == *"true"* ]]; 
    then
	print_uptime
    else 
	echo "0" > /dev/null
    fi

    if [[ $cpu_temp == *"true"* ]]; # if "cpu_temp" in configuration file = "true", print CPU Temperature.
    then
	print_cpu
    else
	echo "0" > /dev/null
    fi

    if [[ $ram_usage == *"true"* ]];
    then
	print_ram
    else
	echo "0" > /dev/null
    fi

    if [[ $swap_usage == *"true"* ]];
    then
	print_swap
    else
	echo "0" > /dev/null
    fi

    if [[ $battery == *"true"* ]];
    then
	print_battery
    else
	echo "0" > /dev/null
    fi
    
#    if [[ $cpu_temp == *"false"* ]] && [[ $ram_usage == *"false"* ]] && [[ $battery == *"false"* ]];
#    then
#	echo "0" > /dev/null
#    else
#	print_line
#    fi

    if [[ $rootfs == *"true"* ]];
    then
	print_storage_rootfs
    else
	echo "0" > /dev/null
    fi

    if [[ $userspace == *"true"* ]];
    then
	print_storage_userspace
    else
	echo "0" > /dev/null
    fi

    if [[ $enable_custom_mountpoint == *"true"* ]];
    then
	print_storage_custom
    else
	echo "0" > /dev/null
    fi

    if [[ $motd == *"true"* ]];
    then
	print_motd
    else
	echo "0" > /dev/null
    fi

    if [[ $enable_custom_entry == *"true"* ]];
    then
	print_custom
    else
	echo "0" > /dev/null
    fi
    
    echo ""
    print_line

    if [[ $lan_ip == *"true"* ]];
    then
	print_lan_ip
    else
	echo "0" > /dev/null
    fi

    if [[ $public_ip4 == *"true"* ]];
    then
	print_wan_ip4
    else
	echo "0" > /dev/null
    fi

    if [[ $public_ip6 == *"true"* ]];
    then
	print_wan_ip6
    else
	echo "0" > /dev/null
    fi
    
    print_line   

#    if [[ $lan_ip == *"false"* ]] && [[ $public_ip4 == *"false"* ]] && [[ $public_ip6 == *"false" ]] 
#    then
#	echo "0" > /dev/null
#    else
#	print_line
#    fi

    echo ""
}

print_looped () { # Loop print_once, option: --loop (or -l) 
    while true
    do
	clear
	print_once
	sleep $update_interval # Update interval in seconds, reading $update_interval from configration file.
    done
}

print_help () {
    echo "usage:"
    echo "--help (-h) : print help and exit"
    echo "--loop (-l) : loop output"
    echo "--generate-config (-gc) : Generate Configuration File and exit"
    echo "--update (-u) : self-update"
    echo "--modules-install (-mi) : select modules to install"
}

# Console Arguments
case $1 in
    "--help" )
	print_help
	;;
    "-h" )
	print_help
	;;
    "--loop" )
	print_looped
	;;
    "-l" )
	print_looped
	;;
    "--generate-config" )
	generate_config
	;;
    "-gc" )
	generate_config
	;;
    "--update" ) update
		 ;;
    "-u" ) update
	   ;;
    "--modules-install" ) modules_install
			  ;;
    "-mi" ) modules_install
	    ;;
    
    * )
	print_once
esac
